package nc.ui.uapbd.featureprimary.handler;

import nc.ui.bd.feature.ref.model.FeatureRefModel;
import nc.ui.pub.beans.UIRefPane;
import nc.ui.pub.bill.BillCardPanel;
import nc.ui.pubapp.uif2app.event.IAppEventHandler;
import nc.ui.pubapp.uif2app.event.card.CardBodyBeforeEditEvent;
import nc.ui.uif2.model.AbstractUIAppModel;
import nc.vo.uapbd.featureprimary.FeaturePrimaryBodyVO;
import nc.vo.uapbd.featureprimary.FeaturePrimaryHeadVO;

public class AceBodyBeforeEditHandler implements IAppEventHandler<CardBodyBeforeEditEvent> {

	private AbstractUIAppModel model = null;
	
	
	public void handleAppEvent(CardBodyBeforeEditEvent e) {
		
		String key = e.getKey();
		int row = e.getRow();
		BillCardPanel billPanel = e.getBillCardPanel();
		/*if(SaleAllocSheetVO.PK_CUSTOMER.equals(key) || SaleAllocSheetVO.PK_SCPRODUCT.equals(key) || SaleAllocSheetVO.PK_PRODUCT.equals(key)){
			UIRefPane curef = (UIRefPane) billPanel.getBodyItem(key).getComponent();
			curef.getRefModel().setPk_org(this.getModel().getContext().getPk_org());
		}*/
		if(FeaturePrimaryBodyVO.PK_FEATURECLASS.equals(key)){
			String defaultfeaclass = billPanel.getHeadItem("pk_material.featureclass").getValue();
//			billPanel.getbodyi
			UIRefPane curef = (UIRefPane) billPanel.getBodyItem(key).getComponent();
//			curef.setWhereString(where);
//			curef.getRefModel().setPk_org(this.getModel().getContext().getPk_org());
		}else if(FeaturePrimaryBodyVO.PK_FEATURE.equals(key)){
			UIRefPane curef = (UIRefPane) billPanel.getBodyItem(key).getComponent();
//			billPanel.getBodyPanel().insertLine((row + 1), 1);
//			curef.removeAll();
			curef.setMultiSelectedEnabled(true);
//			String defaultfeaclass = billPanel.getHeadItem("pk_material.featureclass").getValue();
			String defaultfeaclass = (String) billPanel.getBodyValueAt(row, FeaturePrimaryBodyVO.PK_FEATURECLASS);
			StringBuilder conditionsql = new StringBuilder();
			conditionsql.append(" dr = 0  \n")
//			conditionsql.append(" and cfeatureid in (select b.cfeatureid from bd_feature_b b where b.cfeatureid = (select fc.cfeatureid from bd_fclass_b fc where fc.cfeatureclsid='")
//						.append(defaultfeaclass)
//						.appned("'))");
						.append(" and cfeatureid in (")
						.append("				select distinct(b.cfeatureid) from bd_feature_b b \n")
						.append("				left join bd_fclass_b fc on fc.cfeatureid = b.cfeatureid \n")
						.append("				where fc.cfeatureclsid='" + defaultfeaclass + "' \n")
						.append("				and fc.cfeatureid = b.cfeatureid  \n")
						.append("				 and b.dr = 0 and fc.dr = 0 and fc.fenable = 0 ")
						.append(")");
			curef.setWhereString(conditionsql.toString());
//			curef.setRefValue();
			curef.setCacheEnabled(false);
			
		}
		e.setReturnValue(Boolean.TRUE);
	}
	
	public AbstractUIAppModel getModel() {
		return model;
	}

	public void setModel(AbstractUIAppModel model) {
		this.model = model;
	}
	

}
